package com.tarena.practice.manage.util;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

	private static Document getDoc(String url) {
		Document document = null;
		try {
			document = Jsoup.connect(url).get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static List<Topics> getTopics(){
		List<Topics> topics = new ArrayList<>();
		String url = "http://www.jiakaobaodian.com/kaoshi/6307ac07.html";
		
		try {
			Document document = getDoc(url);
			Elements liEles = document.select("ul.list-w").first().getElementsByTag("li");
			for (Element liEle : liEles) {
				Topics topic = new Topics();
				String timu = liEle.select("div.timu").first().select("p").first().text();
				timu = timu.substring(0, timu.lastIndexOf("查"));
				timu = timu.substring(timu.indexOf('、') + 1);
				String answerUrl = liEle.select("div.timu").first().select("a").first().attr("href");
				topic.setContent(timu);
				topic.setAnswerUrl(answerUrl);
				Elements pEles = liEle.select("div.options-w").first().select("p");
				int index = 0;
				for (Element pEle : pEles) {
					switch (index) {
					case 0:
						String optionA = pEle.text();
						topic.setOptionA(optionA);
						index++;
						break;
					case 1:
						String optionB = pEle.text();
						topic.setOptionB(optionB);
						index++;
						break;
					case 2:
						String optionC = pEle.text();
						topic.setOptionC(optionC);
						index++;
						break;
					case 3:
						String optionD = pEle.text();
						topic.setOptionD(optionD);
						index++;
						break;
					default:
						break;
					}
				}
					
				Element element= liEle.select("div.answer-w").first();
				Elements imgs = element.getElementsByTag("img");
				for (Element img : imgs) {
					topic.setImgUrl(img.attr("src"));
				}
//					topic.setImgUrl(imageUrl);
					
				topics.add(topic);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return topics;
	}
	
	public static String getAnswer(Topics topic){
		String string = "";
		Document document = null;
		try {
//			System.out.println(topic.getAnswerUrl());
			if(topic.getAnswerUrl() == null) {
				return null;
			}
			document = getDoc(topic.getAnswerUrl());
			Element divEle = document.select("div.options-w").first();
			String answer = divEle.select("p.success").first().text().substring(0, 1);
			topic.setAnswer(answer);
			
			Element explain = document.select("div.com-shiti-xiangjie").first().select("div.content").first();
			
			if(explain == null) {
				System.out.println(topic.getContent());
			}
			
			if(explain != null) {
				string = explain.text();
				topic.setExplain(explain.text());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}
	
	public static void main(String[] args) {
		List<Topics> topics = getTopics();
		for (int i = 0; i < topics.size(); i++) {
			Topics topic = new Topics();
			String explain = getAnswer(topics.get(i));
			
		}
		System.out.println(topics);
	}
}
